package model.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Properties;

/**
 * Klasa za uspostavljanje veze sa bazom podataka
 * 
 * @author Hrvoje Torbasinovic
 * @version 0.1
 * @see model.db.DefaultConnection
 */
public class ConnectionDescriptor {
   
   private String driverClassName;
   private String protocol;
   private String host;
   private String port;
   private String serverName;
   private String dbName;
   private String user;
   private String password;
   private String dbLocale;
   private String nodefdac;
   private String ifxIsolationLevel;
   private String ifxLockModeWait;

   private Connection connection;
   
   /**
    * Instancira novi ConnectionDescriptor
    * 
    * @param options mapa u kojoj se nalaze opcije potrebne za
    * spajanje na bazu
    */
   public ConnectionDescriptor(LinkedHashMap<String,String> options)
   {
      super();
      driverClassName = options.get("driverClassName");
      protocol = options.get("protocol");
      host = options.get("host");
      port = options.get("port");
      serverName = options.get("serverName");
      dbName = options.get("dbName");
      user = options.get("user");
      password = options.get("password");
      dbLocale = options.get("DB_LOCALE");
      nodefdac = options.get("NODEFDAC");
      ifxIsolationLevel = options.get("IFX_ISOLATION_LEVEL");
      ifxLockModeWait = options.get("IFX_LOCK_MODE_WAIT");
   }

   /**
    * Ucitava driver
    * 
    * @throws ClassNotFoundException the class not found exception
    */
   public void loadDriver() throws ClassNotFoundException {
      Class.forName(driverClassName);
   }
   
   /**
    * Spaja se na bazu
    * 
    * @throws SQLException the SQL exception
    */
   public void connect() throws SQLException {
	   
      //TODO provjera da li su zadani protocol, host i port
      String url = protocol +  host + ":" + port;
      Properties p = new Properties();
      if (serverName != null)
    	  p.put("INFORMIXSERVER", serverName);
      if (dbName != null)
    	  p.put("database", dbName);
      if (user != null)
    	  p.put("user", user);
      if (password != null)
    	  p.put("password", password);
      if (dbLocale != null)
    	  p.put("DB_LOCALE", dbLocale);
      if (nodefdac != null)
    	  p.put("NODEFDAC", nodefdac);
      if (ifxIsolationLevel != null)
    	  p.put("IFX_ISOLATION_LEVEL", ifxIsolationLevel);
      if (ifxLockModeWait != null)
    	  p.put("IFX_LOCK_MODE_WAIT", ifxLockModeWait);
      connection = DriverManager.getConnection(url, p);
   }
   
   /**
    * Provjerava da li je uspostavljena veza
    * 
    * @return true, if is connected
    */
   public boolean isConnected()
   {
	   if (connection != null) {
	         try 
	         {
	        	 boolean closed = connection.isClosed();
	        	 if (closed)
	        	 {
	        		 return false;
	        	 }
	        	 else
	        	 {
	        		 return true;
	        	 }
	         }catch (SQLException e)
	         {
	        	 return false;
	         }
	   }
	   else
		   return false;
   }

   /**
    * Dohvaca connection objekt.
    * 
    * @return the connection
    */
   public Connection getConnection() {
      if (connection != null) {
         try 
         {
        	 boolean closed = connection.isClosed();
        	 if (closed)
        	 {
        		 return null;
        	 }
        	 else
        	 {
        		 return connection;
        	 }
         }catch (SQLException e)
         {
        	 return null;
         }
      }
      else {
         return null;
      }
   }      
}